<?php
/**
 * Created by PhpStorm.
 * User: SQK
 * Date: 2018/9/17
 * Time: 16:57
 */
namespace app\mobile\controller;
use cmf\controller\HomeBaseController;
use wechat\Https;
class CreatemenuController extends HomeBaseController{
    /**
     * 获取token
     */
    public function getToken(){
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx33b50691375205ff&secret=f8ee806b3d8b13ed2197a833fdfca70d';
        //使用http_request函数发送请求
        $r_url = new Https();
        $str = $r_url->httpRequest($url);
        //使用json_decode对$str进行转移
        $json = json_decode($str);
        //输出access_token
        //echo $json->access_token;
        //定义一个变量保存access_token这个值
        $access_token = $json->access_token;
        return $access_token;
    }
    public function createm(){
        $access_token = $this->getToken();
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}";
        //定义要携带的数据
        $data = '{
            "button":[
            {
                "type":"view",
                "name":"活动",
                "url":"http://www.yunxh1688.com"
            },
            {
                "name":"会员中心",
                "sub_button":[
                {
                    "type":"view",
                    "name":"会员中心",
                    "url":"http://hyxt.w209.madgg.com/mobile/Index/index"
                },
                {
                    "type":"view",
                    "name":"今日油价",
                    "url":"http://www.yunxh1688.com/Wechat/View/login.html"
                }]
            }, 
            {
                "type":"view",
                "name":"在线支付",
                "url":"http://www.yunxh1688.com"
            }]
        }';
        //发送http请求
        $r_url = new Https();
        $str = $r_url->httpRequest($url,'POST',$data);
        //使用json_decode函数进行转义
        $json = json_decode($str);
        //判断是否创建成功
        if($json->errmsg == 'ok'){
            echo '自定义菜单创建成功！';
        }
    }
}